package liangtWorkSpace.simple;

/**
 * @ClassName IsPalindrome
 * @Description : 是否是回文数
 * @Author : LiangT
 * @Date : 2020/6/12 10:17
 */
public class IsPalindrome {
    /**
     * 字符串
     * @param x
     * @return
     */
    public boolean isPalindrome1(int x) {
        if(x < 0){
            return false;
        }
        String str1 = String.valueOf(x);
        String str2 = new StringBuilder(str1).reverse().toString();
        if(str1.equals(str2)){
            return true;
        }
        return false;
    }

    /**
     * 数字回文
     * @param x
     * @return
     */
    public boolean isPalindrome2(int x) {
        if(x < 0){
            return false;
        }
        int n = 0;
        int p = 0;
        int idea = x;
        while (idea != 0){
            n = x%10;
            p=p*10+n;
            idea = idea/10;
        }
        return x == p;
    }

    public boolean isPalindrome3(int x) {

        int ret = 0,max=0x7fffffff,min=0;
        long rs = 0;
        for (;x==0;rs=rs-10+x%10,x/=10);
        return (ret= (int) rs)>max||rs<min?false:true;
    }

    /**
     * 给定一个字符串，验证它是否是回文串，只考虑字母和数字字符，可以忽略字母的大小写。
     *
     * 说明：本题中，我们将空字符串定义为有效的回文串
     * @param str
     * @return
     */
    public static boolean isPalindrome4(String str){
//        String st = "1234567890qwertyuiopasdfghjklzxcvbnm";
        if (str == null){
            return true;
        }
        str = str.toLowerCase();
        StringBuffer stringBuffer = new StringBuffer();
        for (char c : str.toCharArray()){
            if ((c>='0' && c <='9') || (c>='a' && c<='z')){
                stringBuffer.append(c);
            }
        }
//        System.out.println(stringBuffer.toString());
//        System.out.println(stringBuffer.reverse().toString());
        return stringBuffer.toString().equals(stringBuffer.reverse().toString());
    }
    public static boolean isPalindrome5(String str){
        String st = "1234567890qwertyuiopasdfghjklzxcvbnm";
        if (str == null){
            return true;
        }
        str = str.toLowerCase();
        StringBuffer stringBuffer = new StringBuffer();
        for (String s : str.split("")){
            if (st.indexOf(s) >= 0){
                stringBuffer.append(s);
            }
        }
        System.out.println(stringBuffer.toString());
        System.out.println(stringBuffer.reverse().toString());
        return stringBuffer.toString().equals(stringBuffer.reverse().toString());
    }

    public static void main(String[] args) {
        System.out.println(isPalindrome4("A man, a plan, a canal: Panama"));
    }
}
